package com.moody.util;

import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.moody.hibernate.domain.ResultDisplayForFindCustomerInFinancialAnalysis;
/*
 * Copyright © Moody analytics
 *
 * @Author: Wang Fuqiang
 * @Time: 2011-9-7 上午11:57:59
 * @Modified by: 
 * @Description: 日历Util
 */

public class CalendarUtil {
	private static Date now = new Date();	
	
	/*
	 * 获得当前日期
	 */
	public static Date getCurrentDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		try {
			date = sdf.parse(getCurrentDateStr());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/*
	 * 获得当前时间
	 */
	public static Date getCurrentTime(){
		return now;
	}
	
	/*
	 * 获得当前日期 
	 * @Return Param:  String
	 */
	public static String getCurrentDateStr(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(now);
	}
	
	/*
	 * 获得当前时间
	 * @Return Param:  String
	 */
	public static String getCurrentTimeStr(){
		DateFormat d = DateFormat.getDateTimeInstance();
		return d.format(now);
	}
	
	/*
	 * 获取当前年份Date
	 */
	public static Date getCurrentYearDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date date = new Date();
		try {
			date = sdf.parse(getCurrentDateStr());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static String getCurrentYearStr(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date date = new Date();
		String dateStr = "";
		try {
			dateStr = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateStr;
	}
	
	/*
	 * 获取当前年份Str
	 */
	
	/*
	 * 获取去年String
	 */
	
	public static String getLastYearBeginStr(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date date = new Date();
		String dateStr = "";
		Integer i = 0; 
		try {
			dateStr = sdf.format(date);
			i = Integer.parseInt(dateStr) - 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i+"";
	}
	
	public static String getLastTwoYearStr(){
		return Integer.parseInt(getLastYearBeginStr())-1+"";
	}
	
	public static String getLastThreeYearStr(){
		return Integer.parseInt(getLastTwoYearStr())-1+"";
	}
	/*
	 * 获取去年开始Date
	 */
	
	public static Date getLastYearBeginDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date date = new Date();
		try {
			date = sdf.parse(getLastYearBeginStr());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/*
	 * 获取去年结束Date
	 */
	public static Date getLastYearEndDate(){
		Date date = getCurrentYearDate();
		long time = date.getTime() - 1;
		
		return new Date(time);
	}
	
	
	public static ResultDisplayForFindCustomerInFinancialAnalysis getAverage(List<ResultDisplayForFindCustomerInFinancialAnalysis> list){
		ResultDisplayForFindCustomerInFinancialAnalysis averageResult = new ResultDisplayForFindCustomerInFinancialAnalysis();
		Class resultClass = ResultDisplayForFindCustomerInFinancialAnalysis.class;
		Field[] fieldAll = resultClass.getDeclaredFields();
		LinkedList<Field> fieldList = new LinkedList<Field>();
		
		for(int i = 0; i < fieldAll.length; i++){
			String subName = fieldAll[i].getName().substring(0,3);
			if(subName.equals("FR0") || subName.equals("PFR") || subName.equals("FFR") || subName.equals("SFR")){
				fieldList.add(fieldAll[i]);
			}
		}
		Field[] field = fieldList.toArray(new Field[0]);
		int[] count = new int[field.length];
		double[] averValue = new double[field.length];
		for(ResultDisplayForFindCustomerInFinancialAnalysis cust:list){
			for(int i = 0; i < field.length; i++){
				try {
					String valueString = (String)(field[i].get(cust));
					if(valueString != null && !"".equals(valueString) && !"null".equals(valueString)){
						double value = Double.parseDouble(valueString);
						averValue[i] += value;
						count[i] ++;
					}
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		for(int i = 0; i < field.length; i++){
			try {
				if(count[i] != 0)field[i].set(averageResult, String.valueOf(new DecimalFormat("###,##0.00").format(averValue[i] / count[i])));
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		averageResult.setCustName("<< 平均值 >>");
		return averageResult;
	}
	
	
	/*
	 * 获取前年String
	 */
	
	/*
	 * 获取前年结束Date
	 */
	
	/*
	 * 获取大前年Date
	 */
	public static void main(String[] args){
		//System.out.println(CalendarUtil.getCurrentDate());
		//System.out.println(CalendarUtil.getCurrentDateStr());
		//System.out.println(CalendarUtil.getCurrentTime());
		//System.out.println(CalendarUtil.getCurrentTimeStr());
		System.out.println(CalendarUtil.getLastYearBeginStr());
		System.out.println(CalendarUtil.getLastYearBeginDate());
		System.out.println(CalendarUtil.getCurrentYearStr());
		System.out.println(CalendarUtil.getCurrentYearDate());
		//System.out.println(CalendarUtil.getLastYearEndDate());
	}
}
